﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization;
using System.Collections;
using System.Xml.Serialization;
using System.Security.Cryptography;
using Microsoft.Reporting.WinForms;


namespace ВУИ
{
    public partial class Form1 : Form
    {
        public List<Patient> lPatient;
        public List<EpiAtt> lEpiAtt;
        public List<etiologAtt> lEtiAtt;
        public List<organ> lOrgan;
        public List<Osmotr> lSdn;
        public List<OAKNorm> lON;
        public List<Prirost> lAV;
        public List<Prirost> lAR;
        public login login;
        public int type;
        public fLogin fl;
        public Lab ef;

        
        static string getMd5Hash(string input)
        {
            // Create a new instance of the MD5CryptoServiceProvider object.
            MD5 md5Hasher = MD5.Create();
            // Convert the input string to a byte array and compute the hash.
            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
            // Create a new Stringbuilder to collect the bytes
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();
            // Loop through each byte of the hashed data 
            // and format each one as a hexadecimal string.
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }
            // Return the hexadecimal string.
            return sBuilder.ToString();
        }

        // Verify a hash against a string.
        static bool verifyMd5Hash(string input, string hash)
        {
            // Hash the input.
            string hashOfInput = getMd5Hash(input);
            // Create a StringComparer an compare the hashes.
            StringComparer comparer = StringComparer.OrdinalIgnoreCase;

            if (0 == comparer.Compare(hashOfInput, hash))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public Form1()
        {
            InitializeComponent();
            lPatient = new List<Patient>();
            lEpiAtt = new List<EpiAtt>();
            lEtiAtt = new List<etiologAtt>();
            lOrgan = new List<organ>();
            lSdn = new List<Osmotr>();
            lON = new List<OAKNorm>();
            lAV = new List<Prirost>();
            lAR = new List<Prirost>();
            XmlSerializer xml;
            FileStream stream;

           
            if (File.Exists("Login.xml"))
            {
                xml = new XmlSerializer(typeof(login));
                stream = new FileStream("Login.xml", FileMode.Open, FileAccess.Read);
                login = (login)xml.Deserialize(stream);
                stream.Close();
            }
            else
            {
                login = new ВУИ.login();
                login.adress = "";
                login.db= "";
                login.log= "";
                login.pass = "";
                login.type = true;
            }
            fl = new fLogin(this);
            fl.textBox1.Text = login.log;
            fl.textBox2.Text = login.adress;
            fl.maskedTextBox1.Text = login.pass;
            if (login.type)
                fl.comboBox1.SelectedIndex = 0;
            fl.ShowDialog();
            
            
        }

        public void autosave()
        {
            autosave a = new autosave();
            a.date = DateTime.Now;
            a.log = login;
            XmlSerializer xml;
            FileStream stream;
            xml = new XmlSerializer(typeof(autosave));
            stream = new FileStream("autosave.xml", FileMode.Create, FileAccess.ReadWrite);
            xml.Serialize(stream, a);
            stream.Close();
            Directory.CreateDirectory("temp");

          
            if (type == 0)
            {

                xml = new XmlSerializer(typeof(List<Patient>));
                stream = new FileStream("temp.temp", FileMode.Create, FileAccess.ReadWrite);
                xml.Serialize(stream, lPatient);
                stream.Close();
                EnCrypFile("temp.temp", "temp\\Patient.xml", "iwemc83^KL&H*MF^", "8f7bktq2ehfRJEMC");

                xml = new XmlSerializer(typeof(List<EpiAtt>));
                stream = new FileStream("temp.temp", FileMode.Create, FileAccess.ReadWrite);
                xml.Serialize(stream, lEpiAtt);
                stream.Close();
                EnCrypFile("temp.temp", "temp\\EpiAtt.xml", "iwemc83^KL&H*MF^", "8f7bktq2ehfRJEMC");

                xml = new XmlSerializer(typeof(List<etiologAtt>));
                stream = new FileStream("temp.temp", FileMode.Create, FileAccess.ReadWrite);
                xml.Serialize(stream, lEtiAtt);
                stream.Close();
                EnCrypFile("temp.temp", "temp\\EtiAtt.xml", "iwemc83^KL&H*MF^", "8f7bktq2ehfRJEMC");

                xml = new XmlSerializer(typeof(List<organ>));
                stream = new FileStream("temp.temp", FileMode.Create, FileAccess.ReadWrite);
                xml.Serialize(stream, lOrgan);
                stream.Close();
                EnCrypFile("temp.temp", "temp\\Organ.xml", "iwemc83^KL&H*MF^", "8f7bktq2ehfRJEMC");

                xml = new XmlSerializer(typeof(List<Osmotr>));
                stream = new FileStream("temp.temp", FileMode.Create, FileAccess.ReadWrite);
                xml.Serialize(stream, lSdn);
                stream.Close();
                EnCrypFile("temp.temp", "temp\\Sdn.xml", "iwemc83^KL&H*MF^", "8f7bktq2ehfRJEMC");

                xml = new XmlSerializer(typeof(List<OAKNorm>));
                stream = new FileStream("temp.temp", FileMode.Create, FileAccess.ReadWrite);
                xml.Serialize(stream, lON);
                stream.Close();
                EnCrypFile("temp.temp", "temp\\LON.xml", "iwemc83^KL&H*MF^", "8f7bktq2ehfRJEMC");

                xml = new XmlSerializer(typeof(List<Prirost>));
                stream = new FileStream("temp.temp", FileMode.Create, FileAccess.ReadWrite);
                xml.Serialize(stream, lAV);
                stream.Close();
                EnCrypFile("temp.temp", "temp\\LAV.xml", "iwemc83^KL&H*MF^", "8f7bktq2ehfRJEMC");

                xml = new XmlSerializer(typeof(List<Prirost>));
                stream = new FileStream("temp.temp", FileMode.Create, FileAccess.ReadWrite);
                xml.Serialize(stream, lAR);
                stream.Close();
                EnCrypFile("temp.temp", "temp\\LAR.xml", "iwemc83^KL&H*MF^", "8f7bktq2ehfRJEMC");
             

            }
        }

        public void ref_Lab()
        {
            int top = ef.Top;
            int left = ef.Left;
            ef.Hide();
            ef.Close();
      
            
            if (Convert.ToInt32(DetiTre.SelectedNode.Tag) >= 0)
            {
                ef = new Lab(Convert.ToInt32(DetiTre.SelectedNode.Tag), this);
                ef.Top = top;
                ef.Left = left;
                ef.ShowDialog();
                
            }
        }

        //Процедура обновляющие все визуальные компоненты при изменени данных
        public void refresh()
        {
            //Обновление списка детей на главной форме
            TreeNode temp;
            DetiTre.Nodes[0].Nodes.Clear();
        
            foreach (Patient p in lPatient)
            {
                if (p.This != null)
                {
                    temp = new TreeNode();
                    temp.Text = p.This.get();
                    temp.Tag = p.id;
                    if ((p.This.Name.ToLower().IndexOf(toolStripTextBox1.Text.Trim().ToLower()) >= 0) || (p.This.Patronymic.ToLower().IndexOf(toolStripTextBox1.Text.Trim().ToLower()) >= 0) || (p.This.Surname.ToLower().IndexOf(toolStripTextBox1.Text.Trim().ToLower()) >= 0) || (toolStripTextBox1.Text.Trim()==""))
                    DetiTre.Nodes[0].Nodes.Add(temp);
                }
            }
           
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {

        }

        public void logining(login log)
        {
            login = log;
            XmlSerializer xml;
            FileStream stream;
            xml = new XmlSerializer(typeof(login));
            stream = new FileStream("Login.xml", FileMode.Create, FileAccess.ReadWrite);
            xml.Serialize(stream, login);
            stream.Close();
            type = 3;
            if (login.type)
            {
                Directory.CreateDirectory("cript");
                if (File.Exists(login.adress+"\\Users.xml"))
                {                
                    xml = new XmlSerializer(typeof(List<users>));
                    stream = new FileStream(login.adress + "\\Users.xml", FileMode.Open, FileAccess.Read);
                    List<users> u = (List<users>)xml.Deserialize(stream);
                    stream.Close();
                    foreach (users t in u)
                    {
                        if (verifyMd5Hash(login.log,t.log) & verifyMd5Hash(login.pass,t.pass))
                        {
                            type=2;
                            if (verifyMd5Hash(login.log + login.pass + "1", t.type)) type = 1;
                            if (verifyMd5Hash(login.log + login.pass + "0", t.type)) type = 0;

                        }
                    }

                }
            }
            if (type < 3)
            {
                fl.Close();
               /* Directory.CreateDirectory("cript");
                foreach (string st in Directory.GetFiles(login.adress))
                {
                    File.Copy(login.adress + "\\" + st, "cript\\" + st);
                }*/

             
                
                xml = new XmlSerializer(typeof(List<Patient>));
                DeCrypFile("temp.temp", login.adress + "\\Patient.xml", "iwemc83^KL&H*MF^", "8f7bktq2ehfRJEMC");
                stream = new FileStream("temp.temp", FileMode.Open, FileAccess.Read);
                lPatient = (List<Patient>)xml.Deserialize(stream);
                stream.Close();
                

                xml = new XmlSerializer(typeof(List<EpiAtt>));
                DeCrypFile("temp.temp", login.adress + "\\EpiAtt.xml", "iwemc83^KL&H*MF^", "8f7bktq2ehfRJEMC");
                stream = new FileStream("temp.temp", FileMode.Open, FileAccess.Read);
                lEpiAtt = (List<EpiAtt>)xml.Deserialize(stream);
                stream.Close();
                
        

                xml = new XmlSerializer(typeof(List<etiologAtt>));
                DeCrypFile("temp.temp", login.adress + "\\EtiAtt.xml", "iwemc83^KL&H*MF^", "8f7bktq2ehfRJEMC");
                stream = new FileStream("temp.temp", FileMode.Open, FileAccess.Read);
                lEtiAtt = (List<etiologAtt>)xml.Deserialize(stream);
                stream.Close();
                
                xml = new XmlSerializer(typeof(List<organ>));
                DeCrypFile("temp.temp", login.adress + "\\Organ.xml", "iwemc83^KL&H*MF^", "8f7bktq2ehfRJEMC");
                stream = new FileStream("temp.temp", FileMode.Open, FileAccess.Read);
                lOrgan = (List<organ>)xml.Deserialize(stream);
                stream.Close();
                

                xml = new XmlSerializer(typeof(List<Osmotr>));
                DeCrypFile("temp.temp", login.adress + "\\Sdn.xml", "iwemc83^KL&H*MF^", "8f7bktq2ehfRJEMC");
                stream = new FileStream("temp.temp", FileMode.Open, FileAccess.Read);
                lSdn = (List<Osmotr>)xml.Deserialize(stream);
                stream.Close();
                
             

                xml = new XmlSerializer(typeof(List<OAKNorm>));
                DeCrypFile("temp.temp", login.adress + "\\LON.xml", "iwemc83^KL&H*MF^", "8f7bktq2ehfRJEMC");
                stream = new FileStream("temp.temp", FileMode.Open, FileAccess.Read);
                lON = (List<OAKNorm>)xml.Deserialize(stream);
                stream.Close();
                
                xml = new XmlSerializer(typeof(List<Prirost>));
                DeCrypFile("temp.temp", login.adress + "\\LAV.xml", "iwemc83^KL&H*MF^", "8f7bktq2ehfRJEMC");
                stream = new FileStream("temp.temp", FileMode.Open, FileAccess.Read);
                lAV = (List<Prirost>)xml.Deserialize(stream);
                stream.Close();
                

                xml = new XmlSerializer(typeof(List<Prirost>));
                DeCrypFile("temp.temp", login.adress + "\\LAR.xml", "iwemc83^KL&H*MF^", "8f7bktq2ehfRJEMC");
                stream = new FileStream("temp.temp", FileMode.Open, FileAccess.Read);
                lAR = (List<Prirost>)xml.Deserialize(stream);
                stream.Close();
                
                refresh();
                timer1.Enabled = true;
            }

        }

        public void loginingauto(login log)
        {
            login = log;
            XmlSerializer xml;
            FileStream stream;
            xml = new XmlSerializer(typeof(login));
            stream = new FileStream("Login.xml", FileMode.Create, FileAccess.ReadWrite);
            xml.Serialize(stream, login);
            stream.Close();
            type = 3;
            if (login.type)
            {
                if (File.Exists(login.adress + "\\Users.xml"))
                {
                    xml = new XmlSerializer(typeof(List<users>));
                    stream = new FileStream(login.adress + "\\Users.xml", FileMode.Open, FileAccess.Read);
                    List<users> u = (List<users>)xml.Deserialize(stream);
                    stream.Close();
                    foreach (users t in u)
                    {
                        if (verifyMd5Hash(login.log, t.log) & verifyMd5Hash(login.pass, t.pass))
                        {
                            type = 2;
                            if (verifyMd5Hash(login.log + login.pass + "1", t.type)) type = 1;
                            if (verifyMd5Hash(login.log + login.pass + "0", t.type)) type = 0;

                        }
                    }

                }
            }
            if (type < 3)
            {
                fl.Close();

                xml = new XmlSerializer(typeof(List<Patient>));
                DeCrypFile("temp.temp", "temp\\Patient.xml", "iwemc83^KL&H*MF^", "8f7bktq2ehfRJEMC");
                stream = new FileStream("temp.temp", FileMode.Open, FileAccess.Read);
                lPatient = (List<Patient>)xml.Deserialize(stream);
                stream.Close();


                xml = new XmlSerializer(typeof(List<EpiAtt>));
                DeCrypFile("temp.temp", "temp\\EpiAtt.xml", "iwemc83^KL&H*MF^", "8f7bktq2ehfRJEMC");
                stream = new FileStream("temp.temp", FileMode.Open, FileAccess.Read);
                lEpiAtt = (List<EpiAtt>)xml.Deserialize(stream);
                stream.Close();



                xml = new XmlSerializer(typeof(List<etiologAtt>));
                DeCrypFile("temp.temp", "temp\\EtiAtt.xml", "iwemc83^KL&H*MF^", "8f7bktq2ehfRJEMC");
                stream = new FileStream("temp.temp", FileMode.Open, FileAccess.Read);
                lEtiAtt = (List<etiologAtt>)xml.Deserialize(stream);
                stream.Close();

                xml = new XmlSerializer(typeof(List<organ>));
                DeCrypFile("temp.temp", "temp\\Organ.xml", "iwemc83^KL&H*MF^", "8f7bktq2ehfRJEMC");
                stream = new FileStream("temp.temp", FileMode.Open, FileAccess.Read);
                lOrgan = (List<organ>)xml.Deserialize(stream);
                stream.Close();


                xml = new XmlSerializer(typeof(List<Osmotr>));
                DeCrypFile("temp.temp", "temp\\Sdn.xml", "iwemc83^KL&H*MF^", "8f7bktq2ehfRJEMC");
                stream = new FileStream("temp.temp", FileMode.Open, FileAccess.Read);
                lSdn = (List<Osmotr>)xml.Deserialize(stream);
                stream.Close();



                xml = new XmlSerializer(typeof(List<OAKNorm>));
                DeCrypFile("temp.temp", "temp\\LON.xml", "iwemc83^KL&H*MF^", "8f7bktq2ehfRJEMC");
                stream = new FileStream("temp.temp", FileMode.Open, FileAccess.Read);
                lON = (List<OAKNorm>)xml.Deserialize(stream);
                stream.Close();

                xml = new XmlSerializer(typeof(List<Prirost>));
                DeCrypFile("temp.temp", "temp\\LAV.xml", "iwemc83^KL&H*MF^", "8f7bktq2ehfRJEMC");
                stream = new FileStream("temp.temp", FileMode.Open, FileAccess.Read);
                lAV = (List<Prirost>)xml.Deserialize(stream);
                stream.Close();


                xml = new XmlSerializer(typeof(List<Prirost>));
                DeCrypFile("temp.temp", "temp\\LAR.xml", "iwemc83^KL&H*MF^", "8f7bktq2ehfRJEMC");
                stream = new FileStream("temp.temp", FileMode.Open, FileAccess.Read);
                lAR = (List<Prirost>)xml.Deserialize(stream);
                stream.Close();

                refresh();
                timer1.Enabled = true;

            }

        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            Patient p1 = new Patient();
            int n = 0;
            foreach (Patient p in lPatient)
            {
                if (p.id > n)
                    n = p.id;
            }
            p1.id = n + 1;
            p1.Date_of_birth = DateTime.Now;
            p1.Statement_date = DateTime.Now;
            lPatient.Add(p1);
            Pasport pF = new Pasport(p1.id, this);
            pF.ShowDialog();  
            
        }

        public int itnPatietn(int id)
        {
            int n = -1;
            int num=0;
            foreach (Patient p in lPatient)
            {
                if (p.id == id)
                    n = num;
                num++;
            }
            return n;
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
           

        }

        public TreeViewEventArgs DetiTreEvent;
        public void DetiTre_AfterSelect(object sender, TreeViewEventArgs e)
        {
            DetiTreEvent = e;
            if (Convert.ToInt32(e.Node.Tag) >= 0)
            {
                int id = Convert.ToInt32(e.Node.Tag);
                 List<ReportParameter> paramList = new List<ReportParameter>();
                paramList.Add(new ReportParameter("ФИО", lPatient[itnPatietn(id)].This.Surname + " " + lPatient[itnPatietn(id)].This.Name + " " + lPatient[itnPatietn(id)].This.Patronymic));
                paramList.Add(new ReportParameter("Дата", lPatient[itnPatietn(id)].Statement_date.ToShortDateString()));
               
                reportViewer1.LocalReport.SetParameters(paramList);
                List<ReportEdpidemcs> r = new List<ReportEdpidemcs>();

                foreach (etiologAtt e1 in lEtiAtt)
                {
                    ReportEdpidemcs r1 = new ReportEdpidemcs();
                    r1.Призхнак = e1.name;
                    r1.Номер = 1;
                    if (lPatient[itnPatietn(id)].Diagnoz != null)
                    {
                        if (lPatient[itnPatietn(id)].Diagnoz.etiolog.IndexOf(e1.id) >= 0)
                        {
                            r1.Да = "+";
                            r1.Нет = "";

                        }
                        else
                        {
                            r1.Да = "";
                            r1.Нет = "+";
                        }
                    }
                    r.Add(r1);

                }
                this.reportViewer1.LocalReport.DataSources.Clear();
                this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("D", r));
                reportViewer1.RefreshReport();

            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            refresh();
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            if (Convert.ToInt32(DetiTre.SelectedNode.Tag) >= 0)
            {
                Pasport pF = new Pasport(Convert.ToInt32(DetiTre.SelectedNode.Tag), this);
                pF.ShowDialog();
            }
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            formConfig CF = new formConfig(this);
            CF.ShowDialog();
        }

        private void выйтиToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void оПрограммеToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox1 ab = new AboutBox1();
            ab.ShowDialog();
        }

        private void сохранитьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            XmlSerializer xml;
            FileStream stream;
            xml = new XmlSerializer(typeof(List<Patient>));
            stream = new FileStream("Patient.xml", FileMode.Create, FileAccess.ReadWrite);
            xml.Serialize(stream, lPatient);
            stream.Close();
            xml = new XmlSerializer(typeof(List<EpiAtt>));
            stream = new FileStream("EpiAtt.xml", FileMode.Create, FileAccess.ReadWrite);
            xml.Serialize(stream, lEpiAtt);
            stream.Close();
            xml = new XmlSerializer(typeof(List<etiologAtt>));
            stream = new FileStream("EtiAtt.xml", FileMode.Create, FileAccess.ReadWrite);
            xml.Serialize(stream, lEtiAtt);
            stream.Close();
            xml = new XmlSerializer(typeof(List<organ>));
            stream = new FileStream("Organ.xml", FileMode.Create, FileAccess.ReadWrite);
            xml.Serialize(stream, lOrgan);
            stream.Close();
            xml = new XmlSerializer(typeof(List<Osmotr>));
            stream = new FileStream("Sdn.xml", FileMode.Create, FileAccess.ReadWrite);
            xml.Serialize(stream, lSdn);
            stream.Close();
            xml = new XmlSerializer(typeof(List<OAKNorm>));
            stream = new FileStream("LON.xml", FileMode.Create, FileAccess.ReadWrite);
            xml.Serialize(stream, lON);
            stream.Close();
            xml = new XmlSerializer(typeof(List<Prirost>));
            stream = new FileStream("LAV.xml", FileMode.Create, FileAccess.ReadWrite);
            xml.Serialize(stream, lAV);
            stream.Close();
            xml = new XmlSerializer(typeof(List<Prirost>));
            stream = new FileStream("LAR.xml", FileMode.Create, FileAccess.ReadWrite);
            xml.Serialize(stream, lAR);
            stream.Close();
        
            refresh();
        }

        private void открытьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            XmlSerializer xml;
            FileStream stream;
            xml = new XmlSerializer(typeof(List<Patient>));
            stream = new FileStream("Patient.xml", FileMode.Open, FileAccess.Read);
            lPatient = (List<Patient>)xml.Deserialize(stream);
            stream.Close();
            xml = new XmlSerializer(typeof(List<EpiAtt>));
            stream = new FileStream("EpiAtt.xml", FileMode.Open, FileAccess.Read);
            lEpiAtt = (List<EpiAtt>)xml.Deserialize(stream);
            stream.Close();
            refresh();
            
                xml = new XmlSerializer(typeof(List<etiologAtt>));
                stream = new FileStream("EtiAtt.xml", FileMode.Open, FileAccess.Read);
                lEtiAtt = (List<etiologAtt>)xml.Deserialize(stream);
                stream.Close();
                refresh();
                xml = new XmlSerializer(typeof(List<organ>));
                stream = new FileStream("Organ.xml", FileMode.Open, FileAccess.Read);
                lOrgan = (List<organ>)xml.Deserialize(stream);
                stream.Close();

                    xml = new XmlSerializer(typeof(List<Osmotr>));
                 stream = new FileStream("Sdn.xml", FileMode.Open, FileAccess.Read);
                 lSdn = (List<Osmotr>)xml.Deserialize(stream);
                 stream.Close();
                 xml = new XmlSerializer(typeof(List<OAKNorm>));
                 stream = new FileStream("LON.xml", FileMode.Open, FileAccess.Read);
                 lON = (List<OAKNorm>)xml.Deserialize(stream);
                 stream.Close();
                 xml = new XmlSerializer(typeof(List<Prirost>));
                 stream = new FileStream("LAV.xml", FileMode.Open, FileAccess.Read);
                 lAV = (List<Prirost>)xml.Deserialize(stream);
                 stream.Close();
                 xml = new XmlSerializer(typeof(List<Prirost>));
                 stream = new FileStream("LAR.xml", FileMode.Open, FileAccess.Read);
                 lAR = (List<Prirost>)xml.Deserialize(stream);
                 stream.Close();

                refresh();
            
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            if (Convert.ToInt32(DetiTre.SelectedNode.Tag) >= 0)
            {
                Epidem eF = new Epidem(Convert.ToInt32(DetiTre.SelectedNode.Tag), this);
                eF.ShowDialog();
            }
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            if (Convert.ToInt32(DetiTre.SelectedNode.Tag) >= 0)
            {
                Diagn eF = new Diagn(Convert.ToInt32(DetiTre.SelectedNode.Tag), this);
                eF.ShowDialog();
            }
        }

        private void toolStripButton7_Click(object sender, EventArgs e)
        {
            if (Convert.ToInt32(DetiTre.SelectedNode.Tag) >= 0)
            {
                ef = new Lab(Convert.ToInt32(DetiTre.SelectedNode.Tag), this);
                ef.ShowDialog();
            }
        }

        private void toolStripButton8_Click(object sender, EventArgs e)
        {
            if (Convert.ToInt32(DetiTre.SelectedNode.Tag) >= 0)
            {
                Dinam eF = new Dinam(Convert.ToInt32(DetiTre.SelectedNode.Tag), this);
                eF.ShowDialog();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            this.reportViewer1.RefreshReport();
        }

        private void reportViewer1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
        }

        private void bindingSource1_CurrentChanged(object sender, EventArgs e)
        {

        }

        private void toolStripButton9_Click(object sender, EventArgs e)
        {
            if (type == 0)
            {
                
                XmlSerializer xml;
                FileStream stream;
            
                xml = new XmlSerializer(typeof(List<Patient>));
                stream = new FileStream("temp.temp", FileMode.Create, FileAccess.ReadWrite);
                xml.Serialize(stream, lPatient);
                stream.Close();
                EnCrypFile("temp.temp", login.adress + "\\Patient.xml", "iwemc83^KL&H*MF^", "8f7bktq2ehfRJEMC");
             
                xml = new XmlSerializer(typeof(List<EpiAtt>));
                stream = new FileStream("temp.temp", FileMode.Create, FileAccess.ReadWrite);
                xml.Serialize(stream, lEpiAtt);
                stream.Close();
                EnCrypFile("temp.temp", login.adress + "\\EpiAtt.xml", "iwemc83^KL&H*MF^", "8f7bktq2ehfRJEMC");
             
                xml = new XmlSerializer(typeof(List<etiologAtt>));
                stream = new FileStream("temp.temp", FileMode.Create, FileAccess.ReadWrite);
                xml.Serialize(stream, lEtiAtt);
                stream.Close();
                EnCrypFile("temp.temp", login.adress + "\\EtiAtt.xml", "iwemc83^KL&H*MF^", "8f7bktq2ehfRJEMC");
             
                xml = new XmlSerializer(typeof(List<organ>));
                stream = new FileStream("temp.temp", FileMode.Create, FileAccess.ReadWrite);
                xml.Serialize(stream, lOrgan);
                stream.Close();
                EnCrypFile("temp.temp", login.adress + "\\Organ.xml", "iwemc83^KL&H*MF^", "8f7bktq2ehfRJEMC");
             
                xml = new XmlSerializer(typeof(List<Osmotr>));
                stream = new FileStream("temp.temp", FileMode.Create, FileAccess.ReadWrite);
                xml.Serialize(stream, lSdn);
                stream.Close();
                EnCrypFile("temp.temp", login.adress + "\\Sdn.xml", "iwemc83^KL&H*MF^", "8f7bktq2ehfRJEMC");
             
                xml = new XmlSerializer(typeof(List<OAKNorm>));
                stream = new FileStream("temp.temp", FileMode.Create, FileAccess.ReadWrite);
                xml.Serialize(stream, lON);
                stream.Close();
                EnCrypFile("temp.temp", login.adress + "\\LON.xml", "iwemc83^KL&H*MF^", "8f7bktq2ehfRJEMC");
             
                xml = new XmlSerializer(typeof(List<Prirost>));
                stream = new FileStream("temp.temp", FileMode.Create, FileAccess.ReadWrite);
                xml.Serialize(stream, lAV);
                stream.Close();
                EnCrypFile("temp.temp", login.adress + "\\LAV.xml", "iwemc83^KL&H*MF^", "8f7bktq2ehfRJEMC");
             
                xml = new XmlSerializer(typeof(List<Prirost>));
                stream = new FileStream("temp.temp", FileMode.Create, FileAccess.ReadWrite);
                xml.Serialize(stream, lAR);
                stream.Close();
                EnCrypFile("temp.temp", login.adress + "\\LAR.xml", "iwemc83^KL&H*MF^", "8f7bktq2ehfRJEMC");
             

                refresh();
                File.Delete("autosave.xml");
                foreach (string s in Directory.GetFiles("temp"))
                    File.Delete(s);
                Directory.Delete("temp");
            }
        }

        /*
         private void toolStripButton9_Click(object sender, EventArgs e)
        {
            if (type == 0)
            {
                byte[] KEY = System.Text.UTF8Encoding.UTF8.GetBytes("iwemc83^KL&H*MF^");
                byte[] IV = System.Text.UTF8Encoding.UTF8.GetBytes("8f7bktq2ehfRJEMC");
                XmlSerializer xml;
                FileStream stream;
            
                xml = new XmlSerializer(typeof(List<Patient>));
                stream = new FileStream(login.adress + "\\Patient.xml", FileMode.Create, FileAccess.ReadWrite);
                xml.Serialize(stream, lPatient);
                stream.Close();
                xml = new XmlSerializer(typeof(List<EpiAtt>));
                stream = new FileStream(login.adress + "\\EpiAtt.xml", FileMode.Create, FileAccess.ReadWrite);
                xml.Serialize(stream, lEpiAtt);
                stream.Close();
                xml = new XmlSerializer(typeof(List<etiologAtt>));
                stream = new FileStream(login.adress + "\\EtiAtt.xml", FileMode.Create, FileAccess.ReadWrite);
                xml.Serialize(stream, lEtiAtt);
                stream.Close();
                xml = new XmlSerializer(typeof(List<organ>));
                stream = new FileStream(login.adress + "\\Organ.xml", FileMode.Create, FileAccess.ReadWrite);
                xml.Serialize(stream, lOrgan);
                stream.Close();
                xml = new XmlSerializer(typeof(List<Osmotr>));
                stream = new FileStream(login.adress + "\\Sdn.xml", FileMode.Create, FileAccess.ReadWrite);
                xml.Serialize(stream, lSdn);
                stream.Close();
                xml = new XmlSerializer(typeof(List<OAKNorm>));
                stream = new FileStream(login.adress + "\\LON.xml", FileMode.Create, FileAccess.ReadWrite);
                xml.Serialize(stream, lON);
                stream.Close();
                xml = new XmlSerializer(typeof(List<Prirost>));
                stream = new FileStream(login.adress + "\\LAV.xml", FileMode.Create, FileAccess.ReadWrite);
                xml.Serialize(stream, lAV);
                stream.Close();
                xml = new XmlSerializer(typeof(List<Prirost>));
                stream = new FileStream(login.adress + "\\LAR.xml", FileMode.Create, FileAccess.ReadWrite);
                xml.Serialize(stream, lAR);
                stream.Close();

                refresh();
                File.Delete("autosave.xml");
                Directory.Delete("temp");
            }
        }
        */
        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void toolStripButton10_Click(object sender, EventArgs e)
        {
            
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            autosave();
        }

        private void EnCrypFile(string ifile, string ofile, string key, string iv)
        {
            byte[] KEY = System.Text.UTF8Encoding.UTF8.GetBytes(key);
            byte[] IV = System.Text.UTF8Encoding.UTF8.GetBytes(iv);

            if (File.Exists(ifile))
            {
                FileStream stream;
                Aes ral = Aes.Create();
                stream = new FileStream(ifile, FileMode.Open, FileAccess.Read);
                CryptoStream s = new CryptoStream(stream, ral.CreateEncryptor(KEY, IV), CryptoStreamMode.Read);
                FileStream stream1;
                stream1 = new FileStream(ofile, FileMode.Create, FileAccess.Write);
                s.CopyTo(stream1);
                stream1.Close();
                stream.Close();
            }
        }

        private void DeCrypFile(string ofile, string ifile, string key, string iv)
        {
            byte[] KEY = System.Text.UTF8Encoding.UTF8.GetBytes(key);
            byte[] IV = System.Text.UTF8Encoding.UTF8.GetBytes(iv);

            if (File.Exists(ifile))
            {
                FileStream stream;
                Aes ral = Aes.Create();
                stream = new FileStream(ifile, FileMode.Open, FileAccess.Read);
                CryptoStream s = new CryptoStream(stream, ral.CreateDecryptor(KEY, IV), CryptoStreamMode.Read);
                FileStream stream1;
                stream1 = new FileStream(ofile, FileMode.Create, FileAccess.Write);
                s.CopyTo(stream1);
                s.Close();
                stream1.Close();
                stream.Close();

            }
        }

        public void EnCriptDirectory(string dir)
        {
            if (Directory.Exists(dir))
            {
                string[] d = Directory.GetFiles(dir);
                foreach (string s in d)
                {
                    EnCrypFile(s, "temp.temp", "iwemc83^KL&H*MF^", "8f7bktq2ehfRJEMC");
                    File.Replace("temp.temp", s, null);
                    File.Delete("temp.temp");
                }
            }
        }

        public void DeCriptDirectory(string dir)
        {
            if (Directory.Exists(dir))
            {
                string[] d = Directory.GetFiles(dir);
                foreach (string s in d)
                {
                    DeCrypFile(s, "temp.temp", "iwemc83^KL&H*MF^", "8f7bktq2ehfRJEMC");
                    File.Replace("temp.temp", s, null);
                    File.Delete("temp.temp");
                }
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (File.Exists("autosave.xml"))
            File.Delete("autosave.xml");
            if (Directory.Exists("temp"))
            foreach (string s in Directory.GetFiles("temp"))
                File.Delete(s);
            Directory.Delete("temp");
        }

    }
}
